﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using WaterForecast.entity;
using WaterForecast.chart;

namespace WaterForecast.manager
{
    public class ReportData
    {
        public DateTime Time;
        public String 日期 { get; set; }
        public String 时间 { get; set; }
        public Double 朱沱站 { get; set; }
        public Double 石鼓站 { get; set; }
        public Double 桐子林站 { get; set; }
        public Double 高场站 { get; set; }
        public Double 瀑布沟 { get; set; }
        public Double 紫坪铺 { get; set; }
        public Double 平武站 { get; set; }
        public Double 小河坝站 { get; set; }
        public Double 罗渡溪站 { get; set; }
        public Double 乌江渡站 { get; set; }
        public Double 武隆站 { get; set; }
        public Double 富顺站 { get; set; }
        public Double 广元站 { get; set; }
        public Double 武胜站 { get; set; }
        public Double 北碚站 { get; set; }
    }

    public class ReportDataManager
    {
        public List<ReportData> reportList;
        Dictionary<String, RadChartControl> dicChart;

        Dictionary<String, RadChartControl> chartDictionary;

        public ReportDataManager()
        {
            reportList = new List<ReportData>();
            dicChart = new Dictionary<string, RadChartControl>();
            chartDictionary = new Dictionary<string, RadChartControl>();
        }

        public void SetStationFlowDataList(String stationname, List<StationFlowData> flowList)
        {
            foreach (StationFlowData sfd in flowList)
            {
                ReportData tempRD = reportList.Where(o => o.Time == sfd.Time).FirstOrDefault();
                if (tempRD == null)
                {
                    tempRD = new ReportData();
                    tempRD.Time = sfd.Time;
                    tempRD.日期 = tempRD.Time.ToString("yyyy-MM-dd");
                    tempRD.时间 = tempRD.Time.ToString("HH:mm:ss");
                    reportList.Add(tempRD);
                }

                Type temptype = typeof(ReportData);
                PropertyInfo pInfo = temptype.GetProperty(stationname);
                if (pInfo != null)
                {
                    pInfo.SetValue(tempRD, Math.Round(sfd.Flow, 3), null);
                }
            }
        }

        public RadChartControl BuildPredictChart(String stationname, List<StationFlowData> flowList)
        {
            RadChartControl radchart;
            if (dicChart.ContainsKey(stationname) == false)
            {
                radchart = new RadChartControl();
                List<SeriesMapInfo> smiList = new List<SeriesMapInfo>();
                SeriesMapInfo smi1 = new SeriesMapInfo();

                smi1.LegendLabel = "预测数据";
                smi1.AxisName = "Stage1";
                smi1.XFieldName = "Time";
                smi1.XLabelFormat = "#VAL{yy-MM-dd}";
                //smi1.XMinValue = flowList.FirstOrDefault().Time.ToOADate();
                //smi1.XMaxValue = flowList.LastOrDefault().Time.ToOADate();
                //smi1.XStep = (smi1.XMaxValue - smi1.XMinValue) / 5;

                smi1.YFieldName = "Flow";
                smi1.YLabelFormat = "#VAL{0.00}";
                smi1.YMaxValue = flowList.OrderBy(o => o.Flow).LastOrDefault().Flow + 100;
                smi1.YMinValue = flowList.OrderBy(o => o.Flow).FirstOrDefault().Flow - 10;
                smi1.YStep = (smi1.YMaxValue - smi1.YMinValue) / 5;
                //smi1.ISYLableShow = true;
                smiList.Add(smi1);

                radchart.ChartInit(stationname + "-五日预测", smiList.ToArray(), new ChartConfig() { ShowPointMarks = true, AxisXAutoRange = true, AxisXLayoutMode = Telerik.Windows.Controls.Charting.AxisLayoutMode.Auto });

                dicChart.Add(stationname, radchart);
            }
            else
            {
                radchart = dicChart[stationname];
            }
            radchart.rtChart.ItemsSource = null;
            radchart.rtChart.ItemsSource = flowList;
            return radchart;
        }

        public RadChartControl BuildPredictChart(String stationname, List<CompareFlowData> flowList)
        {
            RadChartControl radchart;
            if (chartDictionary.ContainsKey(stationname) == false)
            {
                radchart = new RadChartControl();

                Double forcastedFlowMaxValue = flowList.OrderBy(o => o.ForcastedFlow).LastOrDefault().ForcastedFlow;
                Double realFlowMaxValue = flowList.OrderBy(o => o.RealFlow).LastOrDefault().RealFlow;

                Double forcastedFlowMinValue = flowList.OrderBy(o => o.ForcastedFlow).FirstOrDefault().ForcastedFlow;
                Double realFlowMinValue = flowList.OrderBy(o => o.RealFlow).FirstOrDefault().RealFlow;
                
                List<SeriesMapInfo> smiList = new List<SeriesMapInfo>();
                SeriesMapInfo smi1 = new SeriesMapInfo();

                smi1.LegendLabel = "预测数据";
                smi1.AxisName = "Stage1";
                smi1.XFieldName = "Time";
                smi1.XLabelFormat = "#VAL{yy-MM-dd}";
                smi1.YFieldName = "ForcastedFlow";
                smi1.YLabelFormat = "#VAL{0.00}";

                smi1.YMaxValue = (forcastedFlowMaxValue > realFlowMaxValue ? forcastedFlowMaxValue : realFlowMaxValue) + 100;
                smi1.YMinValue = (forcastedFlowMinValue < realFlowMinValue ? forcastedFlowMinValue : realFlowMinValue) - 10;
                smi1.YStep = (smi1.YMaxValue - smi1.YMinValue) / 15;
                //smi1.ISYLableShow = true;
                smiList.Add(smi1);

                SeriesMapInfo smi2 = new SeriesMapInfo();

                smi2.LegendLabel = "实际数据";
                smi2.AxisName = "Stage1";
                smi2.XFieldName = "Time";
                smi2.XLabelFormat = "#VAL{yy-MM-dd}";
                smi2.YFieldName = "RealFlow";
                smi2.YLabelFormat = "#VAL{0.00}";
                smi2.YMaxValue = (forcastedFlowMaxValue > realFlowMaxValue ? forcastedFlowMaxValue : realFlowMaxValue) + 100;
                smi2.YMinValue = (forcastedFlowMinValue < realFlowMinValue ? forcastedFlowMinValue : realFlowMinValue) - 10;
                smi2.YStep = (smi2.YMaxValue - smi2.YMinValue) / 15;
                //smi2.ISYLableShow = true;
                smiList.Add(smi2);

                radchart.ChartInit(stationname + "-五日预测", smiList.ToArray(), new ChartConfig() { ShowPointMarks = true, AxisXAutoRange = true, AxisXLayoutMode = Telerik.Windows.Controls.Charting.AxisLayoutMode.Auto });

                chartDictionary.Add(stationname, radchart);
            }
            else
            {
                radchart = chartDictionary[stationname];
            }
            radchart.rtChart.DefaultView.ChartLegend.Header = "预报结果对比曲线";
            radchart.rtChart.ItemsSource = null;
            radchart.rtChart.ItemsSource = flowList;

            //radchart.rtChart.ItemsSource = 
            return radchart;
        }
    }
}
